package net.htjs.juc.printorder;

import lombok.extern.slf4j.Slf4j;

/**
 * 打印先后顺序 wait & notify版本
 */
@Slf4j
public class WaitNotify {

    static final Object lock = new Object();
    static boolean runned = false;
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                while (!runned) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("1");
            }
        }, "t1");
        Thread t2 = new Thread(() -> {
            synchronized (lock) {
                runned = true;
                lock.notify();
                log.debug("2");
            }
        }, "t2");
        t1.start();
        t2.start();
    }
}
